package com.ebt.m.view.timeline;

import android.content.Context;
import android.content.DialogInterface;
import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.customer.net.json.CustomerDynamicJson;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.utils.DateUtils;

public class InteractionViewNoteCreate extends IView implements View.OnClickListener, IDynamicJson {

    private TextView mTimeView;
    private TextView mDateView;
    private TextView mNoteContentView;
    private TextView mNoteTapeIndicator;

    private CustomerDynamicJson mCustomerDynamicJson;

    public InteractionViewNoteCreate(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.view_interaction_item_note_create, this);
        mTimeView = (TextView) findViewById(R.id.tv_time);
        mDateView = (TextView) findViewById(R.id.tv_date);
        mNoteContentView = (TextView) findViewById(R.id.tv_note_content);
        mNoteTapeIndicator = (TextView) findViewById(R.id.tv_note_tape_indicator);
    }

    public InteractionViewNoteCreate(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InteractionViewNoteCreate(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... obj) {
    }

    @Override
    public void onClick(View v) {
        if (mCustomerDynamicJson.objectFromActiveJson != null && mCustomerDynamicJson.objectFromActiveJson.noteId != null){
            if (TextUtils.isEmpty(mCustomerDynamicJson.objectFromActiveJson.noteContent)){
                return;
            }
            initBottomSheet(mCustomerDynamicJson.objectFromActiveJson.noteContent);
        }
    }

    private BottomSheetDialog mBottomSheetDialog;
    private void initBottomSheet(String des) {
        mBottomSheetDialog = new BottomSheetDialog(getContext());
        View view = View.inflate(getContext(), R.layout.layout_note_detail, null);
        TextView textView = (TextView) view.findViewById(R.id.note_description);
        ImageButton imageButton = (ImageButton) view.findViewById(R.id.action_close);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBottomSheetDialog.dismiss();
            }
        });
        textView.setText(des);

        mBottomSheetDialog.setContentView(view);
        mBottomSheetDialog.show();
        mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mBottomSheetDialog = null;
            }
        });
    }

    @Override
    public void updateByJson(Object o) {
        if (o instanceof CustomerDynamicJson){
            CustomerDynamicJson json = (CustomerDynamicJson)o;
            mCustomerDynamicJson = json;

            try {
                Time time = new Time(Time.getCurrentTimezone());
                time.set(DateUtils.parseTimestampInSecond(json.activeTime));
                mTimeView.setText(time.hour + ":" + ((time.minute + "").length() < 2 ? "0" + time.minute : time.minute));
                if (json.showDate != null && json.showDate) {
                    mDateView.setText((time.month + 1) + "-" + time.monthDay + " " + ConfigData.WEEKSTRS[time.weekDay]);
                }
                if (json.objectFromActiveJson != null && json.objectFromActiveJson.noteContent != null){
                    mNoteContentView.setText(json.objectFromActiveJson.noteContent);
                    setOnClickListener(this);
                    if (TextUtils.isEmpty(json.objectFromActiveJson.noteContent)){
                        mNoteContentView.setVisibility(View.GONE);
                        mNoteTapeIndicator.setVisibility(View.VISIBLE);
                    }else{
                        mNoteTapeIndicator.setVisibility(View.GONE);
                        mNoteContentView.setVisibility(View.VISIBLE);
                    }
                }else{
                    mNoteContentView.setVisibility(View.GONE);
                    mNoteTapeIndicator.setVisibility(View.GONE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
